package demo.util;

import javax.servlet.http.HttpServletRequest;

import org.junit.Test;

/**
 * IP请求处理
 * 
 * @author Administrator
 *
 */
public class IpUtil {

    /**
     * 获取访问IP地址
     * 
     * @param request 访问请求
     * @return IP地址
     */
    public static final String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (!ifRealIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (!ifRealIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (!ifRealIp(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (!ifRealIp(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    private static boolean ifRealIp(String ip) {
        if (ip == null) {
            return false;
        }
        if (ip.trim().length() == 0) {
            return false;
        }
        if ("unknown".equalsIgnoreCase(ip)) {
            return false;
        }
        if ("127.0.0.1".equals(ip)) {
            return false;
        }
        if (ip.split("\\.").length != 4) {
            return false;
        }
        return true;
    }

    /**
     * IP v4 转整型
     * 
     * @param ipAddr
     * @return
     */
    public static int getAddress(String ipAddr) {
        String[] addrs = ipAddr.split(".");
        if (addrs != null && addrs.length == 4) {
            return (Integer.parseInt(addrs[0]) << 24) + (Integer.parseInt(addrs[1]) << 16)
                    + (Integer.parseInt(addrs[2]) << 8) + Integer.parseInt(addrs[3]);
        }
        return 0;
    }

    public static byte[] getAddress(int address) {
        byte[] addr = new byte[4];

        addr[0] = (byte) ((address >>> 24) & 0xFF);
        addr[1] = (byte) ((address >>> 16) & 0xFF);
        addr[2] = (byte) ((address >>> 8) & 0xFF);
        addr[3] = (byte) (address & 0xFF);
        return addr;
    }

    public static String numericToTextFormat(byte[] src) {
        return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
    }

    public static int[] getHostRange(String ipAddr, int subNetMaskDigit) {
        int addr = getAddress(ipAddr);
        int subNetMask = getSubNetMask(subNetMaskDigit);
        int hostBeginAddr = (addr & subNetMask) + 1;
        int hostEndAddr = (addr | ~subNetMask) - 1;
        int[] range = new int[2];
        range[0] = hostBeginAddr;
        range[1] = hostEndAddr;
        return range;
    }

    private static int getSubNetMask(int subNetMaskDigit) {
        int mask = 1;
        for (int i = 1; i < 32; i++) {
            mask <<= 1;
            if (i < subNetMaskDigit) {
                mask += 1;
            }
        }
        return mask;
    }

    public static String getMatchedPrefix(String ipAddr, int subNetDigit) {
        String[] addrs = ipAddr.split(".");
        if (subNetDigit >= 8 && subNetDigit < 16) {
            return addrs[0] + ".";
        }
        if (subNetDigit >= 16 && subNetDigit < 24) {
            return addrs[0] + "." + addrs[1] + ".";
        }
        if (subNetDigit >= 24 && subNetDigit < 32) {
            return addrs[0] + "." + addrs[1] + "." + addrs[2] + ".";
        }
        return null;
    }

    @Test
    public void test() {
        int[] range = getHostRange("180.153.255.112", 28);
        System.out.println(numericToTextFormat(getAddress(range[0])));
        System.out.println(numericToTextFormat(getAddress(range[1])));
    }
}
